//公共
import myHeader from "/pages/headerfoot/pages/header.vue"
//表格
import uniForms from "/uni_modules/uni-forms/components/uni-forms/uni-forms.vue";
//语言
import Lang from '@/utils/lang.js';
//公共公益方法
import commonBenefit from '@/common/commonbenefit.js';
//公共方法
import Base from '@/common/base.js';
//表格
import uDataSelect from "/uni_modules/uni-data-select/components/uni-data-select/uni-data-select.vue";
//微信api
import weChat from '@/lib/wechat.js';
//登录api
import Login from '@/lib/login.js';
//用户
import User from '@/lib/user.js';
//公共底部
import Tabbar from "/pages/headerfoot/pages/benefitfooter.vue";
//cache缓存
import Cache from '@/utils/cache.js';

export default {
	components: {
		myHeader,
		uniForms,
		uDataSelect,
		Tabbar
	},
	data() {
		return {
			informationfilling: Lang.lang.informationfilling,
			lang: Lang.lang, //语言1
			school: [],
			teamclass: [],
			code: [],
			typename:'',//类型名称
			typearr:['我是老师','我是学生'],
			classselect: [{
				value: 1,
				text: Lang.lang.student
			}, {
				value: 2,
				text: Lang.lang.teacher
			}],
			valiFormData: {
				username: '',
				school_id: '',
				user_mobile: '',
				team: '',
				grade_id: '',
				classselect: 1,
			},
			gradelist: [],
			ifteacher:Cache.get('isteam'),//是否老师
			ifcode:Cache.get('isteam')?Cache.get('isteam'):0,//1=老师  2=学生  是否激活码 1=是
			ifshow: true, //是否显示
	 
			//校验规则1
			rules: {
				username: {
					rules: [{
						required: true,
						errorMessage: Lang.lang.namecannotempty,
					}]
				},
				school_id: {
					rules: [{
						required: true,
						errorMessage: Lang.lang.schoolcannotempty,
					}]
				},
				team: {
					rules: [{
						required: true,
						errorMessage: Lang.lang.gradecannotempty,
					}]
				},
				user_mobile: {
					rules: [{
						required: true,
						errorMessage: Lang.lang.mobilecannotempty,
					}]
				}
			},
		}
	},
	// 分享到朋友
	onShareAppMessage(res) {
		weChat.onShareAppMessage(Lang.lang.benefittitle, Lang.lang.ShareAppMessagedesc, commonBenefit
			.automaticurl(
				'benefitindex'));
	},
	// 分享到朋友圈
	onShareTimeline(res) {
		weChat.onShareTimeline(Lang.lang.benefittitle, commonBenefit.automaticurl('benefitindex'));
	},
	onShow() {
		var that = this;
		that.ifreglogin();
		//获取用户信息
		that.userinfo();
		//获取等级
		that.getGradeList();
		//默认学生与老师
		that.valiFormData.classselect=that.classselect[that.ifcode].value;
		that.typename=that.classselect[that.ifcode].text;
	},
	methods: {
 
		//改变
		classselectchange(e) {
			console.log(e);
			var that = this;
			//点击我是学生
			if (e == '1') {
				that.ifcode = 0;
				that.valiFormData.classselect=that.classselect[0].value;
				that.typename=that.classselect[0].text;
			}
			//点击我是老师
			if (e == '0') {
				that.ifcode = 1;
				that.valiFormData.classselect=that.classselect[1].value;
				that.typename=that.classselect[1].text;
			}
		},
		//判断是否需要注册或者登录与其他
		async ifreglogin() {
			var that = this;
			//注册
			await Base.globalauthorization();
			//获取学校列表
			await commonBenefit.getSchoolListApiMethod().then(res => {
				if (res) {
					that.school = res;
				}
			});
		},
		//获取等级
		getGradeList() {
			var that = this;
			var data = {};
			commonBenefit.getGradeListApiMethod(data).then(res => {
				if (res) {
					that.gradelist = res.data
				}
			});
		},
		//获取用户信息
		userinfo() {
			var that = this;
			var data = {};
			commonBenefit.getUserApiMethod(data).then(res => {
				if (res) {
					that.ifshow = false;
					that.valiFormData.grade_id = res.grade_id;
					that.valiFormData.username = res.username;
					that.valiFormData.user_mobile = res.user_mobile;
					that.valiFormData.school_id = res.school_id;
					that.valiFormData.code = res.code; //激活码
					//获取学校班级     
					if (parseInt(res.school_id)) {
						that.teamclass = [];
						var data = {};
						data['school_id'] = res.school_id;
						commonBenefit.getTeamListApiMethod(data).then(ress => {
							if (ress) {
								that.teamclass = ress;
								that.valiFormData.team = res.team;
							}
						});
					}
				}
			});
		},
		//等级
		gradechange(e) {
			var that = this;
			that.valiFormData.grade_id = e;
		},
		//学校改变
		schoolchange(e) {
			var that = this;
			that.teamclass = [];
			that.valiFormData.team = '';
			//获取班级
			var school_id = e;
			if (school_id) {
				var data = {};
				data['school_id'] = school_id;
				commonBenefit.getTeamListApiMethod(data).then(res => {
					if (res) {
						that.teamclass = res;
					}
				});
			}
		},
		//班级改变
		teamchangee(e) {
			console.log("e:", e);
		},

		async numberDecryption(e) {
			var that = this;
			var code = await Login.getcode();
			//传参
			var data = {};
			data['code'] = code;
			var backmsg = await User.getJscodenApi(data);
			if (backmsg.data.data.code == '1') {
				//传参
				var data = {};
				var seesionkey = backmsg.data.data.data.session_key;
				data['sessionKey'] = encodeURIComponent(seesionkey);
				data['encryptedData'] = encodeURIComponent(e.encryptedData);
				data['iv'] = encodeURIComponent(e.iv);
				commonBenefit.getUserPhoneApiMethod(data).then(res => {
					if (res) {
						that.valiFormData.user_mobile = res.purePhoneNumber;
					}
				});
			} else {
				//错误提示
				Base.publicshowtoasterror(Lang.lang.infoError);
			}
		},
		//手机
		getPhoneNumber(e) {
			var that = this;
			that.valiFormData.user_mobile = "";
			//電話   
			that.numberDecryption(e.detail);
		},
		//提交
		submit(ref) {
			this.$refs[ref].validate().then(res => {
				//学生信息
				Cache.set('gradeid', '')
				commonBenefit.InFormationFillingApiMethod(res);
			}).catch(err => {
				console.log('err', err);
			})
		},
	}
}